package com.test.selenium.util;

import java.io.IOException;

import org.apache.commons.lang3.RandomStringUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.test.selenium.AirSmokeTest;
import com.test.selenium.data.InputTestData;
import com.test.selenium.data.MovementData;
import com.test.selenium.data.OutputSheet;
import com.test.selenium.data.Results;

public class VerifyPO {
	private boolean debugMode = false;
	
	private WebDriver driver;
	private String movementUrl;
	private SmokeTestTimeStamp timeStamp;
	private WebDriverWait wait;
	private MovementData movementData;
	private OutputSheet resultSheet;
	private InputTestData inputSheet;
	
	private String consolidationNum;
	private String carrierMasterBillNum;
	private String movementStatus;
	private double testTimeTakenSec;
	
	private String isPass;
	private String  potimestamp;
	private String shippotimestamp;
	
	public VerifyPO(){};
	
	
		
	public Results verifyMovementPo() throws InterruptedException, IOException{
		
		Thread.sleep(3000);
		wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div[2]/ul[3]/li/a"))).click();
		
		Thread.sleep(5000);
		WebElement table = driver.findElement(By.id("billing_charges_grid"));
				
		table.findElement(By.xpath("/html/body/div[4]/div/div/div[3]/div[2]/div/div[3]/div[3]/div/table/tbody/tr[2]")).click();
		
		driver.findElement(By.id("chargeDetail")).click();
		
		mvmtChargeDetails();
		
		System.out.println("PO time stamp" + potimestamp);
		
		String testValue= "Movement PO number  = "+ getCarrierMasterBillNum();
		if ( potimestamp !=null )
		{ 
			isPass = "PASS";
		
		}
		
		else if ( potimestamp == " " )
		{
			isPass = "FAIL";
		}
		
		
		Results poResults = new Results();
		poResults.setArea("Movement");
		poResults.setModule("Verify Movement PO");
		poResults.setValue(testValue);
		poResults.setStatus(isPass);
		poResults.setTimeTaken(Double.toString(testTimeTakenSec));
        
		
		return poResults;
		//resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Verify Movement PO", isPass, testTimeTakenSec, testValue);
		
	}
	
		
	public void mvmtChargeDetails()
	{
	    boolean a = false;
	    try {
	        Alert alert = driver.switchTo().alert();
	        String AlertText = alert.getText();
	        System.out.println(AlertText);
	        alert.accept();
	        a = true;
	    } catch (Exception e) {
	        a = false;
	    }finally {
	        if (a != true) {
	            // take ur screenshot or whatever
	           potimestamp=  driver.findElement(By.id("initial_po_sent_timestamp")).getText();
	        }
	    }

	}
	
	
	public Results verifyShipmentPo() throws InterruptedException, IOException{
		
		
		Thread.sleep(9000);
					
		driver.findElement(By.xpath("/html/body/div[4]/div/div/div[3]/div[2]/div[3]/ul/div/div/li/div/label/a")).click();

		Thread.sleep(2000);
		
		shpmtChargeDetails();
		
		System.out.println("PO time stamp" + shippotimestamp);
		
		SmokeTestShipment smokeTestShipment = new SmokeTestShipment(driver, timeStamp, inputSheet.getShipmentData(), resultSheet);
		
		String testValue= "shipment PO number  = "+smokeTestShipment.getShipmentNumber() +"R1";
		if ( potimestamp !=null )
		{ 
			isPass = "PASS";
		
		}
		
		else if ( potimestamp == " " )
		{
			isPass = "FAIL";
		}
		
		Results poResults = new Results();
		poResults.setArea("Movement");
		poResults.setModule("Verify Movement PO");
		poResults.setValue(testValue);
		poResults.setStatus(isPass);
		poResults.setTimeTaken(Double.toString(testTimeTakenSec));
		
		return poResults;
		//resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Verify Movement PO", isPass, testTimeTakenSec, testValue);
		
	}
		
	

	public void shpmtChargeDetails()
	{
	    boolean a = false;
	    try {
	        Alert alert = driver.switchTo().alert();
	        String AlertText = alert.getText();
	        System.out.println(AlertText);
	        alert.accept();
	        a = true;
	    } catch (Exception e) {
	        a = false;
	    }finally {
	        if (a != true) {
	        	
	        	driver.findElement(By.xpath("/html/body/div[15]/div[2]/div/ul/li[3]/a")).click();
	            // take ur screenshot or whatever
	        	shippotimestamp=  driver.findElement(By.id("initial_po_sent_timestamp")).getText();
	        }
	    }

	}
	
	public String getConsolidationNumber() {
		return this.consolidationNum;
	}
	
	public String getCarrierMasterBillNum() {
		return this.carrierMasterBillNum;
	}
	
	public String getMovementStatus() {
		return this.movementStatus;
	}
}
